package top.infopub.mgr.sts.service.impl;


import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.sts.dao.OrgProQaDao;
import top.infopub.mgr.sts.domain.OrgProQaDomain;
import top.infopub.mgr.sts.domain.QaConsoleDomain;
import top.infopub.mgr.sts.service.OrgProQaService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.sts.OrgProQa;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;


@Service
public class OrgProQaServiceImpl extends GenericServiceImpl<OrgProQa, String> implements OrgProQaService {

    @Autowired
    private OrgProQaDao orgProQaDao;

    @Override
    public GenericDao<OrgProQa, String> getDao() {
        return orgProQaDao;
    }

    @Override
    public List<QaConsoleDomain> selectQaProByWhere(Date stsMonth, String hrOrgProId) {
        Map<String, Object> where = Maps.newHashMap();
        where.put("stsMonthStart", stsMonth);
        Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
        where.put("stsMonthEnd", stsMonthEnd);
        where.put("hrOrgProId", hrOrgProId);
        return orgProQaDao.selectQaConsoleByWhere(where);
    }

    @Override
    public Page<QaConsoleDomain> selectProTargetByWhere(Page<QaConsoleDomain> page, Date stsMonth,
                                                        String hrOrgAreaId, String aimType) {
        Map<String, Object> where = Maps.newHashMap();
        where.put("stsMonthStart", stsMonth);
        Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
        where.put("stsMonthEnd", stsMonthEnd);
        where.put("aimType", aimType);
        where.put("hrOrgAreaId", hrOrgAreaId);
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<QaConsoleDomain>)orgProQaDao.selectQaConsoleByWhere(where);
    }

    @Override
    public List<QaConsoleDomain> selectQaRateByWhere(Date stsMonth, String hrOrgProId) {
        Map<String, Object> where = Maps.newHashMap();
        where.put("stsMonthStart", stsMonth);
        Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
        where.put("stsMonthEnd", stsMonthEnd);
        where.put("hrOrgProId", hrOrgProId);
        return orgProQaDao.selectQaRateByWhere(where);
    }

    @Override
    public List<QaConsoleDomain> selectQaAmountByWhere(Integer belongsYear, String hrOrgProId,
                                                       String aimType) {
        Map<String, Object> where = Maps.newHashMap();
        where.put("belongsYear", belongsYear);
        where.put("aimType", Integer.parseInt(aimType));
        where.put("hrOrgProId", hrOrgProId);
        return orgProQaDao.selectQaAmountByWhere(where);
    }

    @Override
    public List<OrgProQaDomain> selectQaByWhere(Date monthStart, Date monthEnd, String type,
                                                String orgId, String orgProName) {
        Map<String, Object> where = Maps.newHashMap();
        where.put("stsMonthStart", monthStart);
        Date stsMonthEnd = DateUtils.getMonthEnd(monthEnd);
        where.put("stsMonthEnd", stsMonthEnd);
        if ("2".equals(type)) {//区域公司
            where.put("hrOrgAreaId", orgId);
        }
        else if ("3".equals(type)) {//项目公司
            where.put("hrOrgProId", orgId);
        }
        where.put("proName", orgProName);
        List<OrgProQaDomain> orgProQaDomains = orgProQaDao.selectQaByWhere(where);
        return orgProQaDomains;
    }

}